/*
 * object-manager.jsp页面专用js文件
 * 前置：common/script/customize/customize.js
 */

function showPage(typeCode){
    var tmpPath = typePage[typeCode];
    var url = isBlank(tmpPath)?basePath+'pages/object/object-empty.jhtml':basePath+ tmpPath+"?typeCode="+typeCode;
    $('#objectFrame').attr('src',url);

}

var typePage = {
    'mainCompany':'pages/object/object-company.jhtml'
    ,'ourCompany':'pages/object/object-company.jhtml'
    ,'upCompany':'pages/object/object-company.jhtml'
    ,'downCompany':'pages/object/object-company.jhtml'
};

function initObjectDialog(operator){
    //先清除表单数据
    $('#objectForm').form('clear');

    var objectTypeId = $('#objectTypeCombo').combo('getValue');
    if(objectTypeId == ""){$.messager.alert('注意','请先选择类别','warning',emptyFn); return false;}

    var selectNode = tmsObject.treeObject($('#objectTree'),'getSelected');
    // var selectNode = $('#objectTree').tree('getSelected');
    if(selectNode == null){$.messager.alert('注意','请先选择对象','warning',emptyFn); return false;}
    selectTreeObject = selectNode.info;

    $('#selectObjectTip').text(selectNode.text);
    if(operator == 'add'){
        $('#addFormDiv').css('display','block');
        $('#folderDiv').css('display','block');
        if(selectTreeObject.parentId == 0){
            //当选择的对象是最高父级的时候就进行锁定下拉框操作
            $('#addMode').combobox('select',1);
            $('#addMode').combobox('disable');
        }else{
            $('#addMode').combobox('enable');
        }

        objectSubmitMode = 'add';
    }else if(operator == 'edit'){
        if(selectTreeObject.parentId == 0){
            $.messager.alert('注意','不能修改类别（最高父级）信息','error',emptyFn); return false;
        }

        $('#folderDiv').css('display','none');
        $('#addFormDiv').css('display','none');
        $('#objectCode').val(selectTreeObject.objectCode);
        $('#objectName').val(selectTreeObject.objectName);

        objectSubmitMode = 'edit';
    }
}

function submitObjectForm(){
    // var reloadNode = tmsObject.treeObject($('#objectTree'),'getSelected');
    // var reloadNode = $('#objectTree').tree('getSelected');
    var param = getJSONFormData('objectForm');
    if(objectSubmitMode == 'add'){
        var parentId = 0;
        if($('#addMode').combobox('getValue') == 0){ //新增同辈
            parentId = selectTreeObject.parentId;
            // reloadNode = $('#objectTree').tree('getParent',reloadNode.target);
            // reloadNode = tmsObject.treeObject($('#objectTree'),'getParent',reloadNode.target);
        }else{ //新增子项
            parentId = selectTreeObject.objectId;
        }


        param.parentId=parentId;
        tmsAjax({
            url:basePath+'objectAction/saveObject.json'
            ,data:JSON.stringify(param)
            ,type:'post'
            ,success:function(data){
                $('#objectDialog').dialog('close');
                // tmsObject.treeObject($('#objectTree'),'reload',tmsObject.treeObject($('#objectTree'),'getParent',reloadNode.target).target);
                // $('#objectTree').tree('reload',$('#objectTree').tree('getParent',reloadNode.target).target);
            }
            ,error:function () {
                alert('error');
            }
        });
    }else if(objectSubmitMode == 'edit'){
        param.objectId=selectTreeObject.objectId;
        tmsAjax({
            url:basePath+'objectAction/updateObject.json'
            ,data:JSON.stringify(param)
            ,type:'post'
            ,success:function(data){
                $('#objectDialog').dialog('close');
                // tmsObject.treeObject($('#objectTree'),'reload',tmsObject.treeObject($('#objectTree'),'getParent',reloadNode.target).target);
                // $('#objectTree').tree('reload',$('#objectTree').tree('getParent',reloadNode.target).target);
            }
            ,error:function () {
                alert('error');
            }
        });
    }
}